package com.eliaukjq.xunlianying.duoxianchengjichu;

/**
 * @Author : 佩萁
 * Description :
 * @Create : 2024/7/20 - 19:35
 */
public class JoinExample {
    public static void main(String[] args) {
        Thread threadB = new Thread(() -> {
            try {
                // 模拟线程B执行一些任务，耗时5秒
                Thread.sleep(5000);
                System.out.println("Thread B has finished.");
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        });

        Thread threadA = new Thread(() -> {
            try {
                System.out.println("Thread A is waiting for Thread B to complete...");
                threadB.start(); // 启动线程B
                threadB.join(2000); // 等待线程B完成，最多等待2秒
                System.out.println("Thread A continues to execute.");
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        });

        threadA.start();
    }
}
